package edu.java.texbooks.scjp.collections.test07.set;

import java.util.Comparator;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Set;
import java.util.SortedSet;
import java.util.TreeSet;

import edu.java.texbooks.scjp.collections.test07.set.student.Student;
import edu.java.texbooks.scjp.collections.test07.set.student.StudentComparatorByMarks;

public class TestTreeSet {
	static Student student1 = new Student("Pesho Ivanov", 56214, 5.34);
	static Student student2 = new Student("Asen Ivanov", 56378, 5.12);

	static Student student3 = new Student("Petq Milcheva", 46214, 3.34);
	static Student student4 = new Student("Asq Ivanova", 67378, 4.12);
	static Student student5 = new Student("Lora Vasileva", 54623, 5.80);

	public static void main(String[] args) {

		// test01();

		test02();

	}

	public static void test01() {
		SortedSet<Student> tss = new TreeSet<Student>();
		tss.add(student1);
		tss.add(student2);
		tss.add(student3);
		tss.add(student4);
		tss.add(student5);

		System.out.println(tss);

		Student ceilingStudent = ((TreeSet<Student>) tss).ceiling(student4);
		System.out.println("Student name: " + ceilingStudent.getName());

		System.out.println("type of ordering: " + tss.comparator());
		System.out.println(student2.getName() + "is in? --> "
				+ tss.contains(student2));

		System.out.println("\n--------------------------------\n");
		Student stud1 = null;
		Student stud2 = null;
		System.out.println("Aim student: " + student5);

		// floor and ceiling
		stud1 = ((TreeSet<Student>) tss).ceiling(student5);
		stud2 = ((TreeSet<Student>) tss).floor(student5);
		System.out.println(stud1.getName());
		System.out.println(stud2.getName());

		// higher() method
		System.out.println("\n--------------------------------\n");
		System.out.println("Aim student: " + student5);
		stud1 = ((TreeSet<Student>) tss).higher(student5);
		if (stud1 != null) {
			System.out.println("Higher student: " + stud1.getName());
		}

		// lower() method
		stud1 = ((TreeSet<Student>) tss).lower(student5);
		if (stud1 != null) {
			System.out.println("Lower student: " + stud1.getName());
		}

		// first()--> Returns the first (lowest) element currently in this set.
		// last()--> Returns the last (highest) element currently in this set.
		System.out.println("\n--------------------------------\n");
		System.out.println("Lowest Student:  " + tss.first().getName());
		System.out.println("Highest Student:  " + tss.last().getName());

		// Iterator
		Iterator<Student> iterator = tss.iterator();
		// Student student = iterator.next();
		// iterator.remove();
		// System.out.println(student.getName());

		System.out.println("\n------ Print names --------\n");
		while (iterator.hasNext()) {
			System.out.println(iterator.next().getName());
		}

		// pollFirst()
		// Retrieves and removes the first (lowest) element, or returns null if
		// this set is empty.

		Student firstStudent = ((TreeSet<Student>) tss).pollFirst();
		if ((firstStudent != null) && (firstStudent instanceof Student)) {
			System.out.println(firstStudent.getName());
		}

		System.out
				.println("\n------After replacement of the first element-----\n");
		iterator = tss.iterator();
		while (iterator.hasNext()) {
			System.out.println(iterator.next().getName());
		}
		System.out.println("------------------------\n");

		// pollLast()
		// Retrieves and removes the last (highest) element, or returns null if
		// this set is empty.

		Student lastStudent = ((TreeSet<Student>) tss).pollLast();
		if ((lastStudent != null) && (lastStudent instanceof Student)) {
			System.out.println("Last student: " + lastStudent.getName());
		}

		System.out
				.println("\n-------- After replacement of the last element -----\n");
		iterator = tss.iterator();
		while (iterator.hasNext()) {
			System.out.println(iterator.next().getName());
		}

		// adding all students again
		tss.clear();
		System.out.println("\n------------------------\n");
		System.out.println(tss);
		Set<Student> hss = new HashSet<Student>();
		hss.add(student1);
		hss.add(student2);
		hss.add(student3);
		hss.add(student4);
		hss.add(student5);

		tss.addAll(hss);
		System.out.println(tss);

	}

	public static void test02() {
		StudentComparatorByMarks scm = new StudentComparatorByMarks();
		TreeSet<Student> tss = new TreeSet<Student>(scm);

		tss.add(student1);
		tss.add(student2);
		tss.add(student3);
		tss.add(student4);
		tss.add(student5);

		System.out.println("\n" + tss);

		// get Comparator
		Comparator<Student> myComparator = (Comparator<Student>) tss
				.comparator();
		if ((myComparator != null)
				&& (myComparator instanceof StudentComparatorByMarks)) {
			System.out.println("OK");
		}

		// descending Iterator
		System.out.println("\n--------------------------");
		Iterator<Student> reversedIterator = tss.descendingIterator();
		while (reversedIterator.hasNext()) {
			System.out.println(reversedIterator.next().getName());
		}
		System.out.println("\n--------------------------");

	}
}
